﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ToolPositioning.CUiTPJumpToolWindow
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.ToolPositioningInternal;
using System;
using System.CodeDom.Compiler;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Markup;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;
using Tecnomatix.Engineering.Ui.WPF;
using UiInternalControls.WPF;


namespace DnProcessSimulateCommands.ToolPositioning;

public class CUiTPJumpToolWindow : TxWindow, IGraphicsPreview, IComponentConnector
{
  private CApTPJumpToolViewModel m_model;
  private const string COMPATIBILITY_CHECK_TYPE = "CompatibilityCheckType";
  private const string COMPATIBILITY_CHECK_SIZE = "CompatibilityCheckSize";
  private const string COMPATIBILITY_SIZE_TOLERANCE = "CompatibilitySizeTolerance";
  private const string COMPATIBILITY_EXPANDED_STATE = "CompatibilityExpanded";
  private const string EXPANDED_STATE = "Expanded";
  private const string CLASSIFICATION_VISIBLE = "ClassifyResult";
  private bool _isClosed;
  internal TxObjEditBoxControl m_toolCtrl;
  internal TxObjEditBoxControl m_fastenerCtrl;
  internal Expander m_compatibilityExpander;
  internal CheckBox m_checkType;
  internal CheckBox m_checkSize;
  internal Expander m_gvExpander;
  internal TxGraphicControl m_gv;
  internal Button m_flipBtn;
  internal Button m_applyBtn;
  private bool _contentLoaded;

  public CUiTPJumpToolWindow()
  {
    this.InitializeComponent();
    this.m_model = new CApTPJumpToolViewModel((IGraphicsPreview) this);
    this.m_model.CheckCPUSupportRequiredInstructions();
    ((FrameworkElement) this).DataContext = (object) this.m_model;
  }

  private void CloseButton_Click(object sender, RoutedEventArgs e) => ((Window) this).Close();

  private void TxWindow_Loaded(object sender, RoutedEventArgs e)
  {
    this._isClosed = false;
    this.m_toolCtrl.SetValidator((ITxValidator) new CApTPObjectValidator(new Func<ITxObject, bool>(this.IsUniqueTool), StringTable.MSG_MISSING_TOOL, StringTable.MSG_INVALID_TOOL, StringTable.MSG_DUPLICATE));
    this.m_fastenerCtrl.SetValidator((ITxValidator) new CApTPObjectValidator(new Func<ITxObject, bool>(this.IsUniqueFastener), StringTable.MSG_MISSING_FASTENER, StringTable.MSG_INVALID_FASTENER, StringTable.MSG_DUPLICATE));
    this.m_toolCtrl.Focus();
    this.RestorePersistencySettings();
  }

  private void RestorePersistencySettings()
  {
    object obj1;
    TxWindowSettings.RestoreWindowValue((TxWindow) this, "CompatibilityCheckType", ref obj1);
    this.m_model.CompatibilityCheckType = obj1 != null && Convert.ToBoolean(obj1);
    TxWindowSettings.RestoreWindowValue((TxWindow) this, "CompatibilityCheckSize", ref obj1);
    this.m_model.CompatibilityCheckSize = obj1 != null && Convert.ToBoolean(obj1);
    TxWindowSettings.RestoreWindowValue((TxWindow) this, "CompatibilitySizeTolerance", ref obj1);
    this.m_model.SizeMatchTolerance = obj1 != null ? Convert.ToInt32(obj1) : 0;
    object obj2;
    TxWindowSettings.RestoreWindowValue((TxWindow) this, "CompatibilityExpanded", ref obj2);
    this.m_compatibilityExpander.IsExpanded = obj2 != null && Convert.ToBoolean(obj2);
    object obj3;
    TxWindowSettings.RestoreWindowValue((TxWindow) this, "Expanded", ref obj3);
    this.m_gvExpander.IsExpanded = obj3 != null && Convert.ToBoolean(obj3);
    object obj4;
    TxWindowSettings.RestoreWindowValue((TxWindow) this, "ClassifyResult", ref obj4);
    this.m_model.ShowClassifyResult = obj4 != null && Convert.ToBoolean(obj4);
  }

  private void StorePersistencySettings()
  {
    TxWindowSettings.StoreWindowValue((TxWindow) this, "CompatibilityExpanded", (object) this.m_compatibilityExpander.IsExpanded);
    TxWindowSettings.StoreWindowValue((TxWindow) this, "CompatibilityCheckType", (object) this.m_model.CompatibilityCheckType);
    TxWindowSettings.StoreWindowValue((TxWindow) this, "CompatibilityCheckSize", (object) this.m_model.CompatibilityCheckSize);
    TxWindowSettings.StoreWindowValue((TxWindow) this, "CompatibilitySizeTolerance", (object) this.m_model.SizeMatchTolerance);
    TxWindowSettings.StoreWindowValue((TxWindow) this, "Expanded", (object) this.m_gvExpander.IsExpanded);
  }

  private bool IsUniqueTool(ITxObject tool) => tool == null || this.m_fastenerCtrl.Object != tool;

  private bool IsUniqueFastener(ITxObject fastener)
  {
    return fastener == null || this.m_toolCtrl.Object != fastener;
  }

  private void m_toolCtrl_Picked(object sender, TxObjEditBoxCtrl_PickedEventArgs args)
  {
    if (this.m_gvExpander.IsExpanded || this.m_toolCtrl.Object == null)
      return;
    this.m_fastenerCtrl.Focus();
  }

  private void TxWindow_Closed(object sender, EventArgs e)
  {
    this._isClosed = true;
    this.m_model.Clear();
    this.StorePersistencySettings();
  }

  private void fastenerCtrl_GotFocus(object sender, RoutedEventArgs e)
  {
    this.m_model.FastenerFocused();
  }

  private void toolCtrl_GotFocus(object sender, RoutedEventArgs e) => this.m_model.ToolFocused();

  private void ApplyButton_Click(object sender, RoutedEventArgs e)
  {
    this.m_model.Apply();
    this.m_fastenerCtrl.Focus();
  }

  private void ResetButton_Click(object sender, RoutedEventArgs e)
  {
    this.m_model.Reset();
    this.m_fastenerCtrl.Focus();
  }

  private void FlipButton_Click(object sender, RoutedEventArgs e)
  {
    this.m_model.FlipTool();
    this.m_fastenerCtrl.Focus();
  }

  private void Expander_StateChanged(object sender, RoutedEventArgs e)
  {
    ((FrameworkElement) this).MinHeight = 0.0;
    ((FrameworkElement) this).MaxHeight = double.PositiveInfinity;
  }

  private void TxWindow_LayoutUpdated(object sender, EventArgs e)
  {
    ((Window) this).SizeToContent = SizeToContent.Height;
    ((FrameworkElement) this).MinHeight = ((FrameworkElement) this).MaxHeight = ((FrameworkElement) this).ActualHeight;
  }

  public Bitmap GetSnapshot(System.Drawing.Size size)
  {
    return this._isClosed ? (Bitmap) null : this.m_gv.GetSnapshot(size);
  }

  public void AdjustCameraPosition(TxVector position, bool doRefresh = false)
  {
    if (this._isClosed)
      return;
    this.m_gv.AdjustCameraPosition(position, doRefresh);
  }

  public void Refresh()
  {
    if (this._isClosed)
      return;
    this.m_gv.Refresh();
  }

  public void AddObject(ITxObject obj)
  {
    if (this._isClosed)
      return;
    this.m_gv.AddObject(obj);
  }

  public void RemoveObject(ITxObject obj)
  {
    if (this._isClosed)
      return;
    this.m_gv.RemoveObject(obj);
  }

  public GraphicControlPreviewState KeepState()
  {
    return this._isClosed ? (GraphicControlPreviewState) null : this.m_gv.KeepState();
  }

  public void RestoreState(GraphicControlPreviewState state)
  {
    if (this._isClosed)
      return;
    this.m_gv.RestoreState(state);
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  public void InitializeComponent()
  {
    if (this._contentLoaded)
      return;
    this._contentLoaded = true;
    Application.LoadComponent((object) this, new Uri("/DnProcessSimulateCommands;component/toolpositioning/cuitpjumptoolwindow.xaml", UriKind.Relative));
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IComponentConnector.Connect(int connectionId, object target)
  {
    switch (connectionId)
    {
      case 1:
        ((FrameworkElement) target).Loaded += new RoutedEventHandler(this.TxWindow_Loaded);
        ((Window) target).Closed += new EventHandler(this.TxWindow_Closed);
        ((UIElement) target).LayoutUpdated += new EventHandler(this.TxWindow_LayoutUpdated);
        break;
      case 2:
        this.m_toolCtrl = (TxObjEditBoxControl) target;
        this.m_toolCtrl.Picked += new TxObjEditBoxCtrl_PickedEventHandler(this.m_toolCtrl_Picked);
        ((UIElement) this.m_toolCtrl).GotFocus += new RoutedEventHandler(this.toolCtrl_GotFocus);
        break;
      case 3:
        this.m_fastenerCtrl = (TxObjEditBoxControl) target;
        ((UIElement) this.m_fastenerCtrl).GotFocus += new RoutedEventHandler(this.fastenerCtrl_GotFocus);
        break;
      case 4:
        this.m_compatibilityExpander = (Expander) target;
        this.m_compatibilityExpander.Expanded += new RoutedEventHandler(this.Expander_StateChanged);
        this.m_compatibilityExpander.Collapsed += new RoutedEventHandler(this.Expander_StateChanged);
        break;
      case 5:
        this.m_checkType = (CheckBox) target;
        break;
      case 6:
        this.m_checkSize = (CheckBox) target;
        break;
      case 7:
        this.m_gvExpander = (Expander) target;
        this.m_gvExpander.Expanded += new RoutedEventHandler(this.Expander_StateChanged);
        this.m_gvExpander.Collapsed += new RoutedEventHandler(this.Expander_StateChanged);
        break;
      case 8:
        this.m_gv = (TxGraphicControl) target;
        break;
      case 9:
        ((ButtonBase) target).Click += new RoutedEventHandler(this.ResetButton_Click);
        break;
      case 10:
        this.m_flipBtn = (Button) target;
        this.m_flipBtn.Click += new RoutedEventHandler(this.FlipButton_Click);
        break;
      case 11:
        this.m_applyBtn = (Button) target;
        this.m_applyBtn.Click += new RoutedEventHandler(this.ApplyButton_Click);
        break;
      case 12:
        ((ButtonBase) target).Click += new RoutedEventHandler(this.CloseButton_Click);
        break;
      default:
        this._contentLoaded = true;
        break;
    }
  }
}
